import { http } from "@/utils/http";

// 通用响应类型
type Result = {
  code: number;
  message: string;
  data?: any;
  timestamp: string;
};

// 文件/目录项类型
export type FileItem = {
  name: string;
  path: string;
  isDirectory: boolean;
  size?: number;
  modifiedTime: string;
  type: "file" | "directory";
};

// 文件列表响应数据类型
export type FileListData = {
  type: "directory";
  name: string;
  path: string;
  modifiedTime: string;
  items: FileItem[];
  totalCount: number;
  isDirectory: true;
};

// 查看文件列表请求参数类型
export type ViewFilesParams = {
  filePath: string;
  recursive?: boolean;
  showHidden?: boolean;
};

// 查看文件列表响应类型
export type ViewFilesResponse = Result & {
  data: FileListData;
};

// 文件类型枚举
export type FileType = 'score' | 'homepage';

// 频率类型枚举
export type FrequencyType = 'daily' | 'hourly' | 'manual';

// 导入配置类型
export type ImportConfig = {
  id?: number;
  name: string;
  importPath: string;
  fileType: FileType;
  frequency: FrequencyType;
  isEnabled: boolean;
  description?: string;
  importHour: number;
  importMinute: number;
  intervalMinutes?: number;
  maxRetries?: number;
  retryIntervalMinutes?: number;
  createdAt?: string;
  updatedAt?: string;
};

// 创建导入配置请求参数类型
export type CreateImportConfigParams = {
  name: string;
  importPath: string;
  fileType: FileType;
  frequency: FrequencyType;
  isEnabled: boolean;
  description?: string;
  importHour: number;
  importMinute: number;
  intervalMinutes?: number;
  maxRetries?: number;
  retryIntervalMinutes?: number;
};

// 创建导入配置响应类型
export type CreateImportConfigResponse = Result & {
  data: ImportConfig;
};

// 获取导入配置列表响应类型
export type GetImportConfigsResponse = Result & {
  data: ImportConfig[];
};

/** 查看服务器文件列表 */
export const viewFiles = (data: ViewFilesParams) => {
  return http.request<ViewFilesResponse>(
    "post",
    "/api/automation/view-files",
    { data }
  );
};

/** 创建导入配置 */
export const createImportConfig = (data: CreateImportConfigParams) => {
  return http.request<CreateImportConfigResponse>(
    "post",
    "/api/automation/configs",
    { data }
  );
};

/** 获取导入配置列表 */
export const getImportConfigs = () => {
  return http.request<GetImportConfigsResponse>(
    "get",
    "/api/automation/configs"
  );
};

/** 更新导入配置 */
export const updateImportConfig = (id: number, data: CreateImportConfigParams) => {
  return http.request<CreateImportConfigResponse>(
    "put",
    `/api/automation/configs/${id}`,
    { data }
  );
};

/** 删除导入配置 */
export const deleteImportConfig = (id: number) => {
  return http.request<Result>(
    "delete",
    `/api/automation/configs/${id}`
  );
};

// 手动导入比分文件请求参数类型
export type ImportScoreParams = {
  configId: string;
  fileDate: string;
  customFilePath?: string;
};

// 手动导入首页文件请求参数类型
export type ImportHomepageParams = {
  configId: string;
  fileDate: string;
  customFilePath?: string;
};

// 手动导入比分文件响应类型
export type ImportScoreResponse = Result & {
  data?: any;
};

// 手动导入首页文件响应类型
export type ImportHomepageResponse = Result & {
  data?: any;
};

/** 手动导入比分文件 */
export const importScoreFile = (data: ImportScoreParams) => {
  return http.request<ImportScoreResponse>(
    "post",
    "/api/automation/import/score",
    { data }
  );
};

/** 手动导入首页文件 */
export const importHomepageFile = (data: ImportHomepageParams) => {
  return http.request<ImportHomepageResponse>(
    "post",
    "/api/automation/import/homepage",
    { data }
  );
};
